<?php


namespace app\admin\controller\v1;


use app\admin\model\MessageModel;
use app\admin\model\UserModel;
use app\admin\service\MessageService;
use app\admin\Validate\message\AnswerMessageValidate;
use app\admin\Validate\message\GetMessageListValidate;
use app\BaseController;
use app\common\Token;
use app\common\validate\IdMustIntValidate;
use app\common\validate\UpdateStatusByIdValidate;
use think\db\BaseQuery;

class Message extends BaseController
{

    /**
     * 获取列表
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function messageList() {
        Token::checkToken($this->request);
        $param = (new GetMessageListValidate())->validateParam();
        $messageModel = new MessageModel();
        $with = [
            'article' => function(BaseQuery $query){
                $query->visible(['article_title', 'id']);
            },
        ];
        $param['article_id'] = 0;
        $condition = [
            'article_id' => ['article_id', '<>', -1],
        ];
        $result = $messageModel->withPaginate($with, $param, $condition);
        return successJson($result, '获取成功');
    }

    /**
     * 获取博客留言列表
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function blogMessageList() {
        Token::checkToken($this->request);
        $param = (new GetMessageListValidate())->validateParam();
        $param['article_id'] = -1;
        $messageModel = new MessageModel();
        $condition = [
            'article_id' => ['article_id', '=', -1],
        ];
        $result = $messageModel->toPaginate($param, $condition);
        return successJson($result, '获取成功');
    }

    /**
     * 通过id更新对应的审核状态
     * @return \think\response\Json
     */
    public function updateMessageStatus() {
        Token::checkToken($this->request);
        $param = (new UpdateStatusByIdValidate())->validateParam();
        $bool = MessageModel::update($param);
        if($bool){
            return successJson(true, "更新成功");
        }
        return errorJson(60003, "更新失败");
    }

    /**
     * 删除留言
     * @return \think\response\Json
     */
    public function deleteMessage() {
        $param = (new IdMustIntValidate()) -> validateParam();
        $bool = MessageModel::destroy($param['id']);
        if($bool){
            return successJson(true, "删除成功");
        }
        return errorJson(60002, "删除失败");
    }

    /**
     * 回答信息
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function answerMessage() {
        Token::checkToken($this->request);
        $param = (new AnswerMessageValidate())->validateParam();
        $bool = (new MessageService())->answerMessage($param);
        if($bool) {
            return successJson(true, '回复成功');
        }
        return errorJson('60005', '回复失败');
    }
}